package connpool

import (
	"net"
	"time"
)

// 开始自动清理无效连接
func (this *ConnPool) beginConnGC(connlist map[string]net.Conn, interval int64) {
	go func() {
		for {
			list := connlist
			downConns := make([]string, 0)
			for key := range list {
				conn := list[key]
				if _, err := conn.Write([]byte{0}); err != nil { //发送空包判断连接有效性
					downConns = append(downConns, key)
				}
			}
			//移除无效连接CONN
			this.lock.Lock()
			for _, k := range downConns {
				delete(list, k)
			}
			this.lock.Unlock()
			time.Sleep(time.Duration(interval) * time.Millisecond) // 休眠
		}
	}()
}
